#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) Copyright 2010 - BidiX@BidiX.info
#
#
'''
    page - basic page model

    Description : Define the basic behaviour and look of a page
        page structure
            - html_header
            [ - tiny_mce]
                - #container
                  - #user
                  - #page
                      - #page_header
                      - #page_content
                          - #page_menu
                          - #page_main
                      - #page_footer
            - html_footer
        with :
            # a div id
            . a div class
            
        standard parameters
            [ action = display | edit | update | delete | do_delete | add | new ... ]

     Usage : 
         initialize_page()
         format_menu()
         format_main()
     
'''

from google.appengine.ext import webapp
from webob import Request

from bidix import user

class Page(webapp.RequestHandler):
    '''
    classdocs
    '''
    __html_header = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr" dir="ltr">
<head>
    <meta http-equiv="Content-Language" content="fr" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" media="all" type="text/css" href="/css/basic.css" />
    <link rel="stylesheet" media="screen" type="text/css" href="/css/screen.css" />
    <link rel="stylesheet" media="print" type="text/css" href="/css/print.css" />
    <!--<script type="text/javascript" charset="utf-8" src="/js/script.js" ></script>-->
    <!--<link rel="alternate" type="application/rss+xml" title="" href="index.xml" />-->
    <!-- compatibility with iPhone -->
    <link rel="icon" href="/images/favicon.ico" />
    <link rel="apple-touch-icon" href="/images/touch-icon.png" />
    <link rel="shortcut icon" href="/images/favicon.ico" />
    <meta name="viewport" content="width=320; initial-scale=1.0;" />
    <script type="text/javascript">
        myInit = function() {window.scroll(0,1);};
    </script>
    <!-- end compatibility with iPhone -->
    <title>%(title)s</title>
</head>
<body onload="myInit()">
    """
    __container_header = """
    <div id="container" align="center">
        <div id="user">%(display_user)s</div>
    """
    __page_header = """
        <div id="page">
    """
    
    __tiny_mce = """
<script type="text/javascript" src="/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
    mode : "exact",
    elements : "tinymce",
    theme : "advanced",
    convert_urls : false,
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_resizing : true,
    theme_advanced_buttons1_add : "removeformat, code",
    theme_advanced_buttons2_add : "code, hr, charmap, blockquote",
    theme_advanced_buttons3 : "",
    theme_advanced_disable : "help,styleselect, sub, sup, visualaid"

});
</script>
"""    

    __page_content = """
            <div id="page_content">
                <div id="page_menu">
                    %(page_menu)s
                </div>
                <div id="page_main">
                    %(page_main)s
                </div>
            </div>
    """
    __page_footer = """
        </div>
    """
    __container_footer = """
         <div class="footer">%(footer)s</div>
    </div>
    """
    __html_footer = """
    </div>
</body>
</html>
    """


                
    def _format_user(self):
        '''return a <div> with user infos and login, logout url'''
        current_user = user.get_current_user()
        response = ""
        if current_user:
            if current_user.is_admin():
                response += "Admin: "
            else:
                response += "User: "
            response += current_user.nickname() + " | <a href=\"%s\">logout</a>"%user.logout_url(self.request.url)
        else:
            response += "<a href=\"%s\">login</a>"%user.login_url(self.request.url)
        response += ""
        return response
    
    def _write_container(self):
        self.out.write(self.__container_header%({
                        'display_user': self._format_user()
                        }))
        self._write_page()
        self.out.write(self.__container_footer%({
                      'footer': self.footer
                      }))

    def _write_html(self):
        self.response.headers['Content-Type'] = 'text/html'
        
        self.out.write(self.__html_header%({
                      'title': self.title
                      }))
        self._write_container()
        self.out.write(self.__html_footer)
        
    def _write_page(self):
        self.out.write(self.__page_header)
        if self.with_html_editor:
            self.out.write(self.__tiny_mce)
        self.out.write(self.__page_content%{
                    'page_menu': self.format_menu(),
                    'page_main': self.format_main()
                                            })
        self.out.write(self.__page_footer)

    def initialize(self, request, response):
        '''override RequestHandler to provide a hook'''
        webapp.RequestHandler.initialize(self, request, response)
 
        # page attributes
        self.out = response.out
        self.action = self.request.get('action')
        if not self.action:
            self.action = 'display'
        if user.get_current_user() and user.get_current_user().is_admin():
            self.user_role = 'admin'
        else:
            self.user_role = 'user'
        self.with_html_editor = False

        #default page initialization
        self.title = "Titre"
        self.footer = "Copyright &copy; 2010 - <a href=\"http://bidix.info\">BidiX</a>"
        self.initialize_page()

    def initialize_page(self):
        '''Subclass should override'''
        
    def format_menu(self):
        '''Subcall should override
        return: String'''
        return "Page menu"
    
    def format_main(self):
        '''Subcall should override
        return: String'''
        if self.action == 'display':
            main = self.format_main_for_display()
        elif self.action == 'edit':
            main = """
<form method="post" action="%(path)s?action=update">
    %(main)s
</form>
"""%{'path': self.request.path, 'main': self.format_main_for_edit()}
        return main

         
    def get(self, *groups):
        '''
        '''
        self._write_html()
    
        
